package com.hnevc.xiaoming.httpactivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.httpbean.MyLogInBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MyLogIn extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private TextView title_view;
    private EditText et_name;
    private EditText et_password;
    private Button btn_denglu;
    private Button btn_zhuce;
    private String name;
    private String password;
    private Handler handler;
    private MyLogInBean myLogInBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_log_in);
        handler = new Handler();
        initView();
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_view = (TextView) findViewById(R.id.title_view);
        title_view.setText("登錄界面");
        et_name = (EditText) findViewById(R.id.et_name);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_denglu = (Button) findViewById(R.id.btn_denglu);
        btn_zhuce = (Button) findViewById(R.id.btn_zhuce);

        btn_denglu.setOnClickListener(this);
        btn_zhuce.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_denglu:
                boolean submit = submit();
                if (submit) {
                    getLogIn(name, password);
                }
                break;
            case R.id.btn_zhuce:
                startActivity(new Intent(MyLogIn.this,MyRegistered.class));
                break;
        }
    }

    private boolean submit() {
        // validate
        name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "password不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void getLogIn(final String name, final String password) {
        new Thread(new Runnable() {
            @Override
            public void run() {

//                try {
//                    myLogInBean = OkHttpUtil.getOkHttpUtil().sendPostReq("http://anli-wechat.boruiman.com/api/UserServer/user_login", "username="+name+"&password="+password+"", MyLogInBean.class);
//                    Log.e("TAG", "run: "+ myLogInBean.getMsg() );
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(MyLogIn.this, ""+ myLogInBean.getMsg() , Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }

//                try {
//                    JSONObject responseByPOST = OkHttpUtil.getOkHttpUtil()
//                    .getResponseByPOST("http://anli-wechat.boruiman.com/api/UserServer/user_login",
//                    "username=" + name + "&password=" + password + "");
//                    Gson gson = new Gson();
//                    myLogInBean = gson.fromJson(responseByPOST.toString(), MyLogInBean.class);
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(MyLogIn.this, ""+ myLogInBean.getMsg() , Toast.LENGTH_SHORT).show();
//                        }
//                    });
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }

                //创建okhttp请求
                OkHttpClient client = new OkHttpClient();
                //请求主体
                RequestBody formBody = new FormBody.Builder()
                        .add("username",name)
                        .add("password",password)
                        .build();
                Request request = new Request.Builder()
                        .url("http://anli-wechat.boruiman.com/api/UserServer/user_login")
                        .post(formBody)
                        .build();

                //通过OKhttpClient发送请求，并将响应的结果存入回调中
                client.newCall(request).enqueue(new Callback() {
                    //错误信息
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("TAG", "onResponse: 网络异常！" );
                    }

                    //请求得到响应后
                    @Override
                    public void onResponse(Call call, final Response response) throws IOException {

                        if (!response.isSuccessful()){
                            Toast.makeText(MyLogIn.this, "请求失败,服务器异常", Toast.LENGTH_SHORT).show();
                        }

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                try {
                                    myLogInBean = gson.fromJson(response.body().string(), MyLogInBean.class);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                Toast.makeText(MyLogIn.this, "登录成功"+myLogInBean.getItem().getUsername(), Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                });
            }
        }).start();


    }
}